<?
require("config.php");

require($rela_path . "bin/country.php");

$location_status .= " | Edit";

$show_backnav = "order_menu";

$menutype = 2;

if ($action=="delete"){

	$Query = "DELETE FROM $table1 WHERE id=$subid LIMIT 1";
	MyQuery($Query);

	header("location:?id=$id");

}

if ($action=="edit"){
/*
	if (!checkCode("cus_basic",$CustomerCode)) {
		header("location:" . $rela_path . "share/msg.php?v=9008");
		exit();
	}
*/

   $field = array(
	"date",
	"customer_code",
	"company",
	"title",
	"contact",
	"address",
	"zip",
	"city",
	"country",
	"tel",
	"fax",
	"email",
	"website",
	"business",
	"profile",
	);


   $content = array(
	$Date,
	$CustomerCode,
	$Company,
	$Title,
	$Contact,
	$Address,
	$Zip,
	$City,
	$Country,
	$Tel,
	$Fax,
	$Email,
	$Website,
	$Business,
	$Profile,
	);

   $exp = "id=$id";

   myUpdate($table,$field,$content,$exp);


   header("location:" . $rela_path . "share/msg.php?v=26");

   exit();

}

if ($action=="add"){

	if (!checkCode("pro_basic",$ProductCode)) {
		header("location:" . $rela_path . "share/msg.php?v=9007");
		exit();
	}

   $field = array(
	"no",
	"pro_code",
	"pro_name",
	"pro_color",
	"pro_size",
	"unit_price",
	"qty",
	);


   $content = array(
	$Code,
	$ProductCode,
	$ProductName,
	$Color,
	$Size,
	$Price,
	$Qty,
	);

	myInsert($table1,$field,$content);

	header("location:edit.php?id=$id");

	//header("location:" . $rela_path . "share/msg.php?v=26");

	//exit();

}

if (!checkRecord($table,$id)){

	header("location:" . $rela_path . "share/msg.php?v=9002");
	exit();

}


$Query = "SELECT 
	no,
	date,
	customer_code,
	company,
	title,
	contact,
	address,
	zip,
	city,
	country,
	tel,
	fax,
	email,
	website,
	business,
	profile
FROM $table WHERE id=$id";

$r = myQuery($Query);

	$Code=			$r[0][0];
	$Date=			$r[0][1];
	$CustomerCode=	$r[0][2];
	$Company=		$r[0][3];
	$Title=			$r[0][4];
	$Contact=		$r[0][5];
	$Address=		$r[0][6];
	$Zip=			$r[0][7];
	$City=			$r[0][8];
	$Country=		$r[0][9];
	$Tel=			$r[0][10];
	$Fax=			$r[0][11];
	$Email=			$r[0][12];
	$Website=		$r[0][13];
	$Business=		$r[0][14];
	$Profile=		$r[0][15];

if ($cid){

$Query = "SELECT 
	code,
	company,
	title,
	contact,
	address,
	zip,
	city,
	country,
	tel,
	fax,
	email,
	website,
	business,
	profile
FROM cus_basic WHERE id=$cid";

$r = myQuery($Query);

	$CustomerCode=	$r[0][0];
	$Company=		$r[0][1];
	$Title=			$r[0][2];
	$Contact=		$r[0][3];
	$Address=		$r[0][4];
	$Zip=			$r[0][5];
	$City=			$r[0][6];
	$Country=		$r[0][7];
	$Tel=			$r[0][8];
	$Fax=			$r[0][9];
	$Email=			$r[0][10];
	$Website=		$r[0][11];
	$Business=		$r[0][12];
	$Profile=		$r[0][13];
}

//Currency
$Query = "SELECT currency FROM cus_basic WHERE contact='$Contact' AND company='$Company' LIMIT 1";
$c=myQuery($Query);

if (!$c) {
	$currency="usd";
}else{
	$currency = $c[0][0];
}


require($rela_path . "inc/header.php");

?>

<table width="95%" border="0" align="center">
  <tr> 
    <td valign="top"> 
      <div align="right"></div>

<form action="" method="post" name="form1">  

<!--hidden filed-->
<input name="action" type="hidden" value="edit">
<input name="id" type="hidden" value="<?print($id)?>">

<table width="95%" border="0" align="center">
  <tr> 
    <td align="right">

	<input type="image" src="../../images/bt_cancel.gif" width="78" height="23" onclick="Redirect('order_menu.php',1);return false;"> 
	<input type="image" src="../../images/bt_save.gif" width="76" height="22"> 

    </td>
  </tr>
</table>

        <table border="0" cellspacing="0" cellpadding="2">
          <tr> 
            <td class="tdheader">No :</td>
            <td> <input type="text" class="text" name="No" value="<?print($Code)?>" DISABLED> </td>
          </tr>
          <tr> 
            <td class="tdheader">Date :</td>
            <td> <input type="text" class="text" name="Date" value="<?print(substr($Date,0,4) . "-" . substr($Date,4,2) . "-" . substr($Date,6,2))?>"> </td>
          </tr>
          <tr> 
            <td class="tdheader">Customer Code :</td>
            <td> <input type="text" class="text" name="CustomerCode" value="<?print($CustomerCode)?>"> <input type="button" class="button" name="Submit22" value="..." onClick="Redirect('<?print($rela_path)?>listing/customer_list.php?fillform=y',2)"> 
            </td>
          </tr>
          <tr> 
            <td height="3" class="tdheader">Company :</td>
            <td height="3"> <input type="text" class="text" name="Company" size="55"  value="<?print($Company)?>"> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Contact Person :</td>
            <td> 
			
<?
$TitleArray = array(
	"Mr.",
	"Ms.",
	"Mrs.",
);

for ($i=0;$i<count($TitleArray);$i++){

	$active=NULL;
	if ($TitleArray[$i] == $Title) $active="CHECKED";

	print("<input type=radio name=Title value=" . $TitleArray[$i] . " $active>" . substr($TitleArray[$i],0,-1));

}
?>
			  
			  <input type="text" class="text" name="Contact" size="55"  value="<?print($Contact)?>"> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Address :</td>
            <td> <textarea class="text" name="Address" cols="40" rows="5"><?print($Address)?></textarea> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Zip Code :</td>
            <td> <input type="text" class="text" name="Zip" size="55" value="<?print($Zip)?>"> 
            </td>
          </tr>
          <tr> 
            <td height="25" class="tdheader">City :</td>
            <td height="25"> <input type="text" class="text" name="City" size="55" value="<?print($City)?>"> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Country : </td>
            <td><?print(FFPCountry("$Country"))?></td>
          </tr>
          <tr> 
            <td class="tdheader">Tel :</td>
            <td> <input type="text" class="text" name="Tel" size="55" value="<?print($Tel)?>"> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Fax :</td>
            <td> <input type="text" class="text" name="Fax" size="55" value="<?print($Fax)?>"> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Email :</td>
            <td> <input type="text" class="text" name="Email" size="55" value="<?print($Email)?>"> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Website :</td>
            <td> <input type="text" class="text" name="Website" size="55" value="<?print($Website)?>"> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Nature of Business :</td>
            <td> <input type="text" class="text" name="Business" size="55" value="<?print($Business)?>"> 
            </td>
          </tr>
          <tr> 
            <td class="tdheader">Message :</td>
            <td> <textarea class="text" name="Profile" cols="40" rows="5"><?print($Profile)?></textarea> 
            </td>
          </tr>
        </table>
<br><br><br>
<table width="95%" border="0" align="center">
  <tr> 
    <td align="right">

	<input type="image" src="../../images/bt_cancel.gif" width="78" height="23" onclick="Redirect('order_menu.php',1);return false;"> 
	<input type="image" src="../../images/bt_save.gif" width="76" height="22"> 

    </td>
  </tr>
</table>


</form>

        <hr> <br>
<form action="" method="post" name="form2">  

<!--hidden filed-->
<input name="action" type="hidden" value="add">
<input name="Code" type="hidden" value="<?print($Code)?>">
<input name="id" type="hidden" value="<?print($id)?>">

        <table border="0" cellspacing="1" cellpadding="3">
          <tr> 
            <td width="20%"><u><b>Sub Item</b></u></td>
            <td width="80%">&nbsp;</td>
          </tr>
          <tr> 
            <td width="20%" class="tdheader">Product Code :</td>
            <td width="80%"> <input type="text" class="text" name="ProductCode"> 
              <input type="button" class="button" name="Submit22" value="..." onClick="Redirect('<?print($rela_path)?>listing/product_list.php?fillform=y&currency=<?print($currency)?>',2)">  
            </td>
          </tr>
          <tr> 
            <td width="20%" class="tdheader">Product Name :</td>
            <td width="80%"><input type="text" class="text" name="ProductName" size="40">
            </td>
          </tr>
          <tr> 
            <td width="20%" class="tdheader">Unit Price : </td>
            <td width="80%"> <input type="text" class="text" name="Price"> </td>
          </tr>
          <tr> 
            <td width="20%" class="tdheader">Qty :</td>
            <td width="80%"> <input type="text" class="text" name="Qty"> </td>
          </tr>
          <tr> 
            <td width="20%">&nbsp;</td>
            <td width="80%"> <input type="submit" class="button" name="Submit3" value="Add to List"> 
            </td>
          </tr>
        </table>
</form>


      <br> 



<?
$Query = "SELECT 
no,
pro_code,
pro_name,
pro_color,
pro_size,
unit_price,
qty,
id 
FROM $table1 WHERE no='$Code'";
$r=myQuery($Query);

?>


<SCRIPT language=javascript> 
function dodelete(id,subid) 
{ 
if (confirm("Are you sure to delete ?")) 
   go(id,subid); 

    function go(id,subid) 
    { 
        //location.href = self.location + "&action=delete&id=" + id + "&subid=" + subid; 
		location.href = "?action=delete&id=" + id + "&subid=" + subid; 
        //form.submit(); 
    } 
} 
</SCRIPT> 


        <table width="95%" border="0" cellpadding="0" cellspacing="1" class="tb">
          <tr class="tbheader"> 
            <td> <div align="center">No.</div></td>
            <td> <div align="center">Product Code</div></td>
            <td> <div align="center">Product Name</div></td>
            <td> <div align="center">Unit Price</div></td>
            <td> <div align="center">Qty</div></td>
            <td> <div align="center">Amount</div></td>
            <td>&nbsp;</td>
          </tr>

<?
$total_qty = 0;
$total_price = 0;
	
if ($r){
	for ($i=0;$i<count($r);$i++){
	
	$subtotal = $r[$i][5]*$r[$i][6];
	$total_qty += $r[$i][6];
	$total_price += $subtotal;
	?>
          <tr class="tbcontent1"> 
            <td><?print($i+1)?></td>
            <td><?print($r[$i][1])?></td>
            <td><?print($r[$i][2])?></td>
            <td> <div align="right"><?print($r[$i][5])?></div></td>
            <td> <div align="right"><?print($r[$i][6])?></div></td>
            <td> <div align="right"><?print(Number_format($subtotal,2))?></div></td>
            <td> <div align="center"><a href="javascript:Redirect('sub_edit.php?id=<?print($r[$i][7])?>',3)" >edit</a> <a href="javascript:dodelete(<?print($id . "," . $r[$i][7])?>)">delete</a></div></td>
          </tr>
<?}
}?>



          <tr> 
            <td colspan="7" class="tbcontent1" height=30><div align="right"><b>Total Amount : <?print("($currency)" . Number_format($total_price,2))?></b></div></td>
          </tr>
        </table>




   
      </td>

  </tr>
</table>

<br>
<br>
<br>
<?
require($rela_path . "inc/footer.php");
?>
